/*
	jGrouse framework
	Copyright 2007 by Denis Riabtchik 
	All rights reserved.
	
	Licensed under modified BSD license. See license.txt for details
*/

/**
 * Module with Application classes
 * @module jgrouse.widgets.Application
 */
jgrouse.module(
{
	name : 'jgrouse.widgets.Application',
	requires : ['jgrouse.widgets.Container'],
	imports : ['jgrouse.dom.Event', 'jgrouse.dom.Element'],
	body : function(embed)
	{
		eval(embed);
		
		/**
		 * Main application widget
		 * @class jgrouse.widgets.ApplicationContainer
		 * @extends jgrouse.widgets.Container
		 */
		jgrouse.define('jgrouse.widgets.ApplicationContainer', 'jgrouse.widgets.Container', 
			function(embed)
		{
			eval(embed);
			
			return {
				/**
				 * @constructor initialize
				 * @inheritdesc
				 */
				initialize : function(options, styles)
				{
					_sup(this, arguments);
					this.addStyles({width:'100%', height : '100%', overflow:'hidden'});
					this._resizeListener = jgrouse.bindAsEventListener(this, 'windowResized');
					this._body = null;
					this._html = null;
				},
				
				/**
				 * @function render
				 * @inheritdesc
				 */
				render : function()
				{
					_sup(this, arguments);
					Event.addListener(window, 'resize', this._resizeListener);
				},
				
				/**
				 * @function dispose
				 * @inheritdesc
				 */
				dispose : function()
				{
					Event.removeListener(window, 'resize', this._resizeListener);
					_sup(this, arguments);
				},
				
				/**
				 * Handler for window resize event
				 * @function windowResized
				 * @param {DOMEvent} e - event with resize information
				 */
				windowResized : function(e)
				{
					this.layout();
				},
				
				/**
				 * @function layout
				 * @inheritdesc
				 */
				layout : function()
				{
					if (!this._body)
					{
						this._body = document.getElementsByTagName('body')[0];
						this._html = document.getElementsByTagName('html')[0];
					}
					var body = this._body;
					body.style.width = this._html.clientWidth + 'px';
					body.style.height = this._html.clientHeight + 'px';
					var width = this._body.clientWidth;
					var height = this._body.clientHeight;
					this.addStyles({width:width + 'px', height: height + 'px'});
					_sup(this, arguments);
				}
			}	
		});
	},
	
	postInit : function(embed)
	{
		eval(embed);
		
		var html = document.getElementsByTagName('html')[0];
		var body = document.getElementsByTagName('body')[0];
		var bodyMarginLeft = Element.getComputedStyle(body, 'margin-left');
		var bodyMarginRight = Element.getComputedStyle(body, 'margin-right');
		var bodyMarginTop = Element.getComputedStyle(body, 'margin-top');
		var bodyMarginBottom = Element.getComputedStyle(body, 'margin-bottom');
		html.style.overflow = 'hidden';
		body.style.margin = '0px';
		body.style.overflow = 'hidden';
		
		/**
		 * Function that initializes the application widget
		 * @function jgrouse.widgets.ApplicationContainer.init
		 * @param {String} defLocation - which filename to use for startup history entry when URL's hash
		 * is not specified/blank. If specified, defLocation must start with hash
		 */
		jgrouse.widgets.ApplicationContainer.init = function(defLocation)
		{
			var app = new jgrouse.widgets.ApplicationContainer();
			jgrouse.widgets.ApplicationContainer._instance = app;
			var body = document.getElementsByTagName('body')[0];
			var ht = document.getElementById('_jgrouse_history_tracker');
			if (ht)
			{
				ht.parentNode.removeChild(ht);
			}
			var div = document.createElement('div');
			Element.setStyles(div,
				{paddingLeft:bodyMarginLeft, 
				 paddingRight:bodyMarginRight,
				 paddingTop : bodyMarginTop,
				 paddingBottom : bodyMarginBottom,
				 width : '100%',
				 height : '100%'
				}
			)
			Element.moveChildrenToNewParent(div, body);
			body.appendChild(div);
			app.setTemplate(body);
			var controller = new jgrouse.util.BaseController();
			app.bind(new jgrouse.util.MvcDriver(controller));
			body.innerHTML = '';
			app.add(body);
			if (ht)
			{
				body.appendChild(ht);
			}
			app.setDisplayed(false);
			app.setDisplayed(true);
			var loc = window.location.hash;
			if (!loc || loc == '#')
			{
				loc = defLocation;
			}
			if (loc)
			{
				loc = loc.substring(1);
			} 	
			
			jgrouse.require('jgrouse.browser.HistoryTracker', function()
 			{
 		        jgrouse.browser.instance.initHistoryTracker();
 		        jgrouse.util.TaskManager.post(function(){jgrouse.browser.instance.setHistoryEntry(loc)});
            });
			
						
			
		}
	}
});